<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item>
				<el-input v-model="state.queryForm.username" placeholder="用户名" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-input v-model="state.queryForm.mobile" placeholder="手机号" clearable></el-input>
			</el-form-item>
			<el-form-item>
				<el-select v-model="state.queryForm.gender" placeholder="性别" clearable="true">
					<el-option label="男" value="0"></el-option>
					<el-option label="女" value="1"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item>
				<el-button @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button v-auth="'sys:user:save'" type="primary" @click="addOrUpdateHandle()">新增</el-button>
			</el-form-item>
			<el-form-item>
				<el-button v-auth="'sys:user:delete'" type="danger" @click="deleteBatchHandle()">删除</el-button>
			</el-form-item>
			<el-form-item>
				<el-button v-auth="'sys:user:export'" type="success" @click="downloadExcel()">导出</el-button>
			</el-form-item>
		</el-form>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border style="width: 100%"
			@selection-change="selectionChangeHandle">
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>

			<el-table-column label="序号" header-align="center" align="center">
				<template #default="scope">
					{{ scope.row.index + 1 }}
				</template>
			</el-table-column>
			<el-table-column prop="username" label="用户名" header-align="center" align="center"></el-table-column>
			<el-table-column prop="realName" label="姓名" header-align="center" align="center"></el-table-column>
			<el-table-column prop="gender" label="性别" width="70" header-align="center" align="center">
				<template #default="scope">
					<span v-if="scope.row.gender === 0">男</span>
					<span v-else-if="scope.row.gender === 1">女</span>
					<span v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="mobile" label="手机号" show-overflow-tooltip header-align="center"
				align="center"></el-table-column>
			<el-table-column prop="password" label="密码" show-overflow-tooltip header-align="center"
				align="center"></el-table-column>
			<el-table-column prop="email" label="邮箱" show-overflow-tooltip header-align="center"
				align="center"></el-table-column>
			<el-table-column prop="superAdmin" label="是否超级管理员" show-overflow-tooltip header-align="center" align="center"
				:formatter="formatSuperAdmin">
			</el-table-column>
			<el-table-column prop="avatar" label="头像" show-overflow-tooltip header-align="center" align="center">
				<template #default="scope">
					<img :src="def + scope.row.avatar" style="width: 50px; height: 50px" />
				</template>
			</el-table-column>
			<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip header-align="center" align="center"
				width="250"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
				<template #default="scope">
					<el-button v-auth="'sys:user:update'" type="primary" link
						@click="addOrUpdateHandle(scope.row.id)">修改</el-button>
					<el-button v-auth="'sys:user:delete'" type="primary" link
						@click="deleteList(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination :current-page="state.page" :page-sizes="state.pageSizes" :page-size="state.limit"
			:total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="sizeChangeHandle"
			@current-change="currentChangeHandle">
		</el-pagination>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refresh-data-list="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="SysUserIndex">
import { useCrud } from '@/hooks'
import { reactive, ref } from 'vue'
import AddOrUpdate from './add-or-update.vue'
import { IHooksOptions } from '@/hooks/interface'
import { useUserExportApi } from '@/api/sys/user'
import { ElMessage, ElMessageBox, UploadProps } from 'element-plus'
import service from '@/utils/request'

const state: IHooksOptions = reactive({
	dataListUrl: '/sys/user/page',
	deleteUrl: '/sys/user/listDelete',
	queryForm: {
		username: '',
		mobile: '',
		gender: ''
	}
})
const url = import.meta.env.VITE_API_URL
const def = ref(url)

const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.init(id)
}
const formatSuperAdmin = (row: any) => {
	return row.superAdmin === 0 ? '否' : '是'
}

const downloadExcel = () => {
	useUserExportApi(state.queryForm)
	return
}

const handleSuccess: UploadProps['onSuccess'] = (res, file) => {
	if (res.code !== 0) {
		ElMessage.error('上传失败：' + res.msg)
		return false
	}

	ElMessage.success({
		message: '上传成功',
		duration: 500,
		onClose: () => {
			getDataList()
		}
	})
}

const beforeUpload: UploadProps['beforeUpload'] = file => {
	if (file.size / 1024 / 1024 / 1024 / 1024 > 1) {
		ElMessage.error('文件大小不能超过100M')
		return false
	}
	return true
}

// 删除
const deleteList = (key?: number | string, path?: any) => {
	let data: any[] = []
	if (key) {
		data = [{ id: key }]
	} else {
		data = state.dataListSelections ? state.dataListSelections : []

		if (data.length === 0) {
			ElMessage.warning('请选择删除记录')
			return
		}
	}

	ElMessageBox.confirm('确定进行删除操作?', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning'
	})
		.then(() => {
			if (state.deleteUrl) {
				console.log('打印删除地址{}', state.deleteUrl)
				console.log('打印删除参数', data)
				service.post(state.deleteUrl, data).then(() => {
					ElMessage.success('删除成功')
					getDataList()
				})
			}
		})
		.catch(() => { })
}

const { getDataList, selectionChangeHandle, sizeChangeHandle, currentChangeHandle, deleteBatchHandle } = useCrud(state)
</script>
